package com.yhx.controller;

import com.yhx.iservice.IRoleService;
import com.yhx.pojo.Role;
import com.yhx.utils.MessageConstant;
import com.yhx.utils.PageResult;
import com.yhx.utils.QueryPageBean;
import com.yhx.utils.Result;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("role")
public class RoleController {
    @Reference
    private IRoleService iRoleService;
    @PostMapping("/listpage")
    public PageResult listpage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult listpage = iRoleService.listpage(queryPageBean);
            return listpage;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    @PostMapping
    public Result save(@RequestBody Role role){
        try {
            return iRoleService.save(role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_ROLE_FAIL );
        }

    }
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable Integer[] ids){
        try {
            return iRoleService.delete(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }
    @GetMapping("/{id}")
    public Result getByid(@PathVariable int id){
        try {
            Result byid = iRoleService.getByid(id);
            return byid;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    @PutMapping
    public Result update(@RequestBody Role role){
        try {
            return iRoleService.update(role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }


    }
    @GetMapping("role")
    public Result list(){
        try {
            Result list = iRoleService.list();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }
}
